package com.dragonsoftbravo.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.dragonsoftbravo.model.BraviaInfo;
import com.dragonsoftbravo.model.RegInfo;
import com.dragonsoftbravo.model.UserInfoDTO;
import com.google.gson.Gson;

import cn.com.dragonsoftbravo.utils.ExcelUtil;
import cn.com.dragonsoftbravo.utils.IOCloseUtils;
import cn.com.dragonsoftbravo.utils.SystemConfigHolder;
import jodd.http.HttpRequest;

@Controller
@RequestMapping("/")
public class BlobController {

	private boolean isPrd = SystemConfigHolder.instance().getValue("prd").equals("1");

	@RequestMapping("/index")
	public String index(Model model, HttpServletResponse response) {
		if (isPrd) {
			return null;
		}
		return "blob/data";
	}

	@RequestMapping("/send")
	public void send(MultipartFile sendFile) {

		try {
			ExcelUtil excelUtil = new ExcelUtil();
			List<List<String>> list = excelUtil.read(sendFile);
			for (List<String> row : list) {

				String openId = row.get(1);
				String url = "http://wechatcompaign.chinacloudapp.cn/WechatAgencyDBC/GetUserInfo?openid=" + openId;
				String jsonTxt = new String(HttpRequest.get(url).send().bodyBytes(), "UTF-8");
				UserInfoDTO u = new Gson().fromJson(jsonTxt, UserInfoDTO.class);
				System.out.println(u.getOpenId()+"\t"+ u.getCity()+"\t"+u.getProvince());
			}
		} catch (Exception ex) {
			System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXX");
		}
	}

	@RequestMapping("/downloadMemberLevelFileExt")
	public void downloadMemberLevelFileExt(MultipartFile memberLevelFile, MultipartFile keyModelFile,
			MultipartFile blackListFile, HttpServletResponse response) {

		ExcelUtil excelUtil = new ExcelUtil();

		List<List<String>> memberLevelList = excelUtil.read(memberLevelFile);
		List<List<String>> keyModelList = excelUtil.read(keyModelFile);
		List<List<String>> blackListList = excelUtil.read(blackListFile);

		// black list
		List<String> blackList = new ArrayList<String>();
		int blackListFlag = 0;
		for (List<String> list : blackListList) {
			if (blackListFlag++ == 0) {
				continue;
			}
			blackList.add(list.get(0));
		}

		// key List
		List<String> keyList = new ArrayList<String>();
		int keyListFlag = 0;
		for (List<String> list : keyModelList) {
			if (keyListFlag++ == 0) {
				continue;
			}
			keyList.add(list.get(2));
		}

		List<RegInfo> regList = new ArrayList<RegInfo>();
		int keyModelFlag = 0;
		for (List<String> reg : memberLevelList) {
			if (keyModelFlag++ == 0) {
				continue;
			}
			RegInfo info = new RegInfo();
			info.setCid(reg.get(0));
			info.setEightD(reg.get(1));
			info.setModel(reg.get(2));
			info.setCategory(reg.get(3));
			info.setCount(reg.get(4));

			if (blackList.contains(info.getCid())) {
				info.setBlack(reg.get(0));
				info.setPoint(0);
			} else {
				info.setBlack("#N/A");
			}

			if (keyList.contains(info.getEightD())) {

				info.setKeyModel(info.getEightD());

				if (!info.getBlack().equals(info.getCid())) {
					int count = 0;
					try {
						count = Integer.parseInt(info.getCount());
					} catch (Exception ex) {
					}
					info.setPoint(1000 * count);
				}
			} else {

				info.setKeyModel("#N/A");

				if (!info.getBlack().equals(info.getCid())) {
					int count = 0;
					try {
						count = Integer.parseInt(info.getCount());
					} catch (Exception ex) {
					}
					info.setPoint(500 * count);
				}
			}

			regList.add(info);

		}

		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("Sheet1");
		HSSFRow row = sheet.createRow((int) 0);

		HSSFCellStyle style = wb.createCellStyle();

		HSSFCell cell = row.createCell(0);
		cell.setCellValue("Cid");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("Crm Pr Product 8d");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("Crm Pr Product Model");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("Crm Pr Product Category");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("Pr Product Count");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("黑名单");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("重点型号");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("分数");
		cell.setCellStyle(style);

		for (int i = 0; i < regList.size(); i++) {
			row = sheet.createRow((int) i + 1);
			RegInfo info = (RegInfo) regList.get(i);
			row.createCell(0).setCellValue(info.getCid());
			row.createCell(1).setCellValue(info.getEightD());
			row.createCell(2).setCellValue(info.getModel());
			row.createCell(3).setCellValue(info.getCategory());
			row.createCell(4).setCellValue(info.getCount());
			row.createCell(5).setCellValue(info.getBlack());
			row.createCell(6).setCellValue(info.getKeyModel());
			row.createCell(7).setCellValue(info.getPoint());
		}

		sheet.autoSizeColumn(0);
		sheet.autoSizeColumn(1);
		sheet.autoSizeColumn(2);
		sheet.autoSizeColumn(3);
		sheet.autoSizeColumn(4);
		sheet.autoSizeColumn(5);
		sheet.autoSizeColumn(6);
		sheet.autoSizeColumn(7);

		OutputStream os = null;

		try {
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=" + "Final Point-Detail.xls");
			os = response.getOutputStream();
			wb.write(response.getOutputStream());
			os.flush();
		} catch (IOException e) {
		} finally {

			IOCloseUtils.close(os);

			try {
				wb.close();
			} catch (IOException e) {
			}
		}

	}

	@RequestMapping("/downloadMemberLevelReport")
	public void downloadMemberLevelReport(MultipartFile memberLevelFile, MultipartFile keyModelFile,
			MultipartFile blackListFile, HttpServletResponse response) {

		ExcelUtil excelUtil = new ExcelUtil();

		List<List<String>> memberLevelList = excelUtil.read(memberLevelFile);
		List<List<String>> keyModelList = excelUtil.read(keyModelFile);
		List<List<String>> blackListList = excelUtil.read(blackListFile);

		// black list
		List<String> blackList = new ArrayList<String>();
		int blackListFlag = 0;
		for (List<String> list : blackListList) {
			if (blackListFlag++ == 0) {
				continue;
			}
			blackList.add(list.get(0));
		}

		// key List
		List<String> keyList = new ArrayList<String>();
		int keyListFlag = 0;
		for (List<String> list : keyModelList) {
			if (keyListFlag++ == 0) {
				continue;
			}
			keyList.add(list.get(2));
		}

		Map<String, Integer> pointsMap = new HashMap<String, Integer>();

		int keyModelFlag = 0;
		for (List<String> reg : memberLevelList) {

			if (keyModelFlag++ == 0) {
				continue;
			}

			String cid = reg.get(0);
			String eightD = reg.get(1);
			int count = 0;
			try {
				count = Integer.parseInt(reg.get(4));
			} catch (Exception ex) {
			}

			if (!blackList.contains(cid)) {
				if (!pointsMap.containsKey(cid)) {
					int value = 0;

					if (keyList.contains(eightD)) {
						value = count * 1000;
					} else {
						value = count * 500;
					}

					pointsMap.put(cid, value);
				} else {
					int value = 0;
					int point = pointsMap.get(cid);
					if (keyList.contains(eightD)) {
						value = count * 1000;
					} else {
						value = count * 500;
					}

					pointsMap.put(cid, point + value);
				}
			}
		}

		pointsMap = ExcelUtil.sortByValue(pointsMap);

		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("Sheet1");
		HSSFRow row = sheet.createRow((int) 0);

		int i = 0;
		for (String key : pointsMap.keySet()) {
			row = sheet.createRow(i++);
			row.createCell(0).setCellValue(key);
			row.createCell(1).setCellValue(pointsMap.get(key));
			row.createCell(2).setCellValue("产品注册");
		}

		sheet.autoSizeColumn(0);
		sheet.autoSizeColumn(1);
		sheet.autoSizeColumn(2);

		OutputStream os = null;

		try {
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=" + "Final Point.xls");
			os = response.getOutputStream();
			wb.write(response.getOutputStream());
			os.flush();
		} catch (IOException e) {
		} finally {

			IOCloseUtils.close(os);

			try {
				wb.close();
			} catch (IOException e) {
			}
		}

	}

	@RequestMapping("/downloadBraviaFileExt")
	public void downloadBraviaFileExt(MultipartFile braviaFile, MultipartFile braviaPointFile,
			HttpServletResponse response) {

		ExcelUtil excelUtil = new ExcelUtil();

		List<List<String>> braviaFileList = excelUtil.read(braviaFile);
		List<List<String>> pointFileList = excelUtil.read(braviaPointFile);

		Map<String, Integer> pointsMap = new HashMap<String, Integer>();
		int keyListFlag = 0;
		for (List<String> pointItem : pointFileList) {
			if (keyListFlag++ == 0) {
				continue;
			}
			int point = 0;
			try {
				point = Integer.parseInt(pointItem.get(2).split("\\.")[0]);
			} catch (Exception ex) {
			}
			pointsMap.put(pointItem.get(0), point);
		}

		List<BraviaInfo> braviaList = new ArrayList<BraviaInfo>();
		int keyModelFlag = 0;
		for (List<String> reg : braviaFileList) {
			if (keyModelFlag++ == 0) {
				continue;
			}
			BraviaInfo info = new BraviaInfo();
			info.setCid(reg.get(0));
			info.setEightD(reg.get(1));
			info.setModel(reg.get(2));
			info.setCreateOn(reg.get(3));
			info.setCategory(reg.get(4));
			info.setDealer(reg.get(5));
			info.setChannel(reg.get(6));

			int count = 0;
			try {
				count = Integer.parseInt(reg.get(7));
			} catch (Exception ex) {
			}
			info.setCount(count);

			if (pointsMap.keySet().contains(info.getEightD())) {
				int price = pointsMap.get(info.getEightD());
				info.setPrice(price);
				info.setSsChannel("N");
				info.setPoint(price * info.getCount());
			}

			braviaList.add(info);

		}

		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("Sheet1");
		HSSFRow row = sheet.createRow((int) 0);

		HSSFCellStyle style = wb.createCellStyle();

		HSSFCell cell = row.createCell(0);
		cell.setCellValue("Cid");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("Crm Pr Product 8d");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("Crm Pr Product Model");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("Crm Pr Created On");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("Crm Pr Product Category");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("Dealer Name");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("Purchase Channel");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("Pr Product Count");
		cell.setCellStyle(style);
		cell = row.createCell(8);
		cell.setCellValue("Price");
		cell.setCellStyle(style);
		cell = row.createCell(9);
		cell.setCellValue("SS Channel");
		cell.setCellStyle(style);
		cell = row.createCell(10);
		cell.setCellValue("Value");
		cell.setCellStyle(style);

		for (int i = 0; i < braviaList.size(); i++) {
			row = sheet.createRow((int) i + 1);
			BraviaInfo info = braviaList.get(i);
			row.createCell(0).setCellValue(info.getCid());
			row.createCell(1).setCellValue(info.getEightD());
			row.createCell(2).setCellValue(info.getModel());
			row.createCell(3).setCellValue(info.getCreateOn());
			row.createCell(4).setCellValue(info.getCategory());
			row.createCell(5).setCellValue(info.getDealer());
			row.createCell(6).setCellValue(info.getChannel());
			row.createCell(7).setCellValue(info.getCount());
			row.createCell(8).setCellValue(info.getPrice());
			row.createCell(9).setCellValue(info.getSsChannel());
			row.createCell(10).setCellValue(info.getPoint());
		}

		sheet.autoSizeColumn(0);
		sheet.autoSizeColumn(1);
		sheet.autoSizeColumn(2);
		sheet.autoSizeColumn(3);
		sheet.autoSizeColumn(4);
		sheet.autoSizeColumn(5);
		sheet.autoSizeColumn(6);
		sheet.autoSizeColumn(7);
		sheet.autoSizeColumn(8);
		sheet.autoSizeColumn(9);
		sheet.autoSizeColumn(10);

		OutputStream os = null;

		try {
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=" + "Bravia APP-Detail.xls");
			os = response.getOutputStream();
			wb.write(response.getOutputStream());
			os.flush();
		} catch (IOException e) {
		} finally {

			IOCloseUtils.close(os);

			try {
				wb.close();
			} catch (IOException e) {
			}
		}

	}

	@RequestMapping("/downloadBraviaReport")
	public void downloadBraviaReport(MultipartFile braviaFile, MultipartFile braviaPointFile,
			HttpServletResponse response) {

		ExcelUtil excelUtil = new ExcelUtil();

		List<List<String>> braviaFileList = excelUtil.read(braviaFile);
		List<List<String>> pointFileList = excelUtil.read(braviaPointFile);

		Map<String, Integer> pointsMap = new HashMap<String, Integer>();
		int keyListFlag = 0;
		for (List<String> pointItem : pointFileList) {
			if (keyListFlag++ == 0) {
				continue;
			}
			int point = 0;
			try {
				point = Integer.parseInt(pointItem.get(2).split("\\.")[0]);
			} catch (Exception ex) {
			}
			pointsMap.put(pointItem.get(0), point);
		}

		Map<String, Integer> pointReportMap = new HashMap<String, Integer>();
		int keyModelFlag = 0;
		for (List<String> reg : braviaFileList) {
			if (keyModelFlag++ == 0) {
				continue;
			}

			String cid = reg.get(0);
			String eightD = reg.get(1);
			int count = 0;
			try {
				count = Integer.parseInt(reg.get(7));
			} catch (Exception ex) {
			}

			if (!pointReportMap.containsKey(cid)) {
				int value = 0;

				if (pointsMap.keySet().contains(eightD)) {
					value = count * pointsMap.get(eightD);
					pointReportMap.put(cid, value);
				}

			} else {
				int value = 0;
				int point = pointReportMap.get(cid);
				if (pointsMap.keySet().contains(eightD)) {
					value = count * pointsMap.get(eightD);
					pointReportMap.put(cid, point + value);
				}
			}
		}

		pointReportMap = ExcelUtil.sortByValue(pointReportMap);

		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("Sheet1");
		HSSFRow row = sheet.createRow((int) 0);

		int i = 0;
		for (String key : pointReportMap.keySet()) {
			row = sheet.createRow(i++);
			row.createCell(0).setCellValue(key);
			row.createCell(1).setCellValue(pointReportMap.get(key));
			row.createCell(2).setCellValue("信任渠道注册BRAVIA");
		}

		sheet.autoSizeColumn(0);
		sheet.autoSizeColumn(1);
		sheet.autoSizeColumn(2);

		OutputStream os = null;

		try {
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=" + "Final Point Detail.xls");
			os = response.getOutputStream();
			wb.write(response.getOutputStream());
			os.flush();
		} catch (IOException e) {
		} finally {

			IOCloseUtils.close(os);

			try {
				wb.close();
			} catch (IOException e) {
			}
		}

	}

}
